package com.foreveross.action.weixin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.service.LogService;
import com.foreveross.service.OrderInfoService;
import com.foreveross.vo.HrFavorable;
import com.foreveross.vo.OrderInfo;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.weixin.service.PayRefundService;
import com.foreveross.weixin.vo.pay.PayRefundResult;
/**
 * 订单
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/weixin/OrderInfoController")
public class OrderInfoController {

	private Logger log = Logger.getLogger(OrderInfoController.class);

	@Resource
	private OrderInfoService orderInfoService;

	@Resource
	private LogService logService;
	

	@RequestMapping(method = RequestMethod.POST, value = "/getOrderInfoList")
	public void getOrderInfoList(HttpServletRequest request,
			HttpServletResponse response, String weixinId,String payStatus,
			String currentPage,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			int total = (int)orderInfoService.getOrderInfoListCount(weixinId,null, null, null, null,null, payStatus, null, null,null);
			PageVo<OrderInfo> pageVo = new PageVo<OrderInfo>(currentPage,total,pageSize);
			List<OrderInfo> list = orderInfoService.getOrderInfoList(weixinId,null, null, null, null,null, payStatus, null, null,null, pageVo.getpageIndex(), pageVo.getpageSize());
			
			//图片地址转换
			List<OrderInfo> listOut = new ArrayList<OrderInfo>(); 
			for (OrderInfo orderInfo : list){
				List<FileVo> fileVoList = FileVo.getFileVoList(request, orderInfo.getHrFavorable().getImage());
				orderInfo.getHrFavorable().setOther(fileVoList);
				listOut.add(orderInfo);
			}
			
			pageVo.setData(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("我的订单 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("我的订单  查看失败");
			log.error("我的订单 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 查看消息通知详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getOrderInfoById")
	public void getOrderInfoById(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			OrderInfo orderInfo = orderInfoService.getOrderInfoById(id);
			resultVo.setSuccess(true);
			resultVo.setData(orderInfo);
			log.info("我的订单 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("我的订单  查看失败");
			log.error("我的订单 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/refundOrderInfo")
	public void refundOrderInfo(HttpServletRequest request,
			HttpServletResponse response,String appId,String mchId,String apiKey,String certUrl,
			String outTradeNo, String total_fee,String id) {
		ResultVo resultVo = new ResultVo();
		try {
			
			PayRefundResult payRefundResult = PayRefundService.getPayRefundResult(appId, mchId, apiKey, certUrl, outTradeNo, total_fee);
			if("SUCCESS".equals(payRefundResult.getReturn_code()) && 
					"SUCCESS".equals(payRefundResult.getResult_code())){
				List<OrderInfo> orderInfoList = orderInfoService.refundOrderInfoAndPayCostFavorableInfo(id);
				resultVo.setSuccess(true);
				resultVo.setData(orderInfoList);
				log.info("订单缴费退款成功!");
			}else{
				resultVo.setSuccess(false);
				resultVo.setMessage("订单缴费退款失败!");
				log.info("订单缴费退款失败!");
			}
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("订单状态修改成功失败");
			log.error("订单状态修改成功失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/deleteOrderInfo")
	public void deleteOrderInfo(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			orderInfoService.deleteOrderInfo(ids);
			resultVo.setSuccess(true);
			log.info("我的订单 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("我的订单 删除失败");
			log.error("我的订单 删除失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
}
